package gov.kqqzyyy.monkey.ydhl.ui;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import adapter.PlanExecuteAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import mytools.MyBaseActivity;
import mytools.MyServiceUtil;
import objects.JJQXYY;
import objects.OrderDetail;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PlanExecuteActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemLongClickListener, RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.oe_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.oe_tb_brxm)
    TextView mTextView;
    @BindView(R.id.oe_swipe)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.oe_expand)
    ExpandableListView mExpandableListView;
    @BindView(R.id.filter_radiogroup)
    RadioGroup mRadioGroup;

    private List<JJQXYY> mYYList;
    private Call<String> jjyyCall;

    private String ChooseTMBH;
    private String ChooseHDBZ;

    private PlanExecuteAdapter mPEAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_execute);
        ButterKnife.bind(this);

        setSwipeRefreshLayout(swipeRefreshLayout);

        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);

        mPEAdapter = new PlanExecuteAdapter(this);
        mExpandableListView.setAdapter(mPEAdapter);
        mExpandableListView.setOnItemLongClickListener(this);

        mRadioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        OrderDetail orderDetail = (OrderDetail) mPEAdapter.getGroup(position);
        ChooseTMBH = orderDetail.getTMBH();
        ChooseHDBZ = orderDetail.getHDBZ();
        int yzzt = Integer.valueOf(orderDetail.getYZZT());
        showChooseDialog(yzzt);
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case 1300:
            case 1301:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    //根据医嘱状态 弹出操作对话框
    private void showChooseDialog(final int yzzt) {
        String[] items = new String[0];
        switch (yzzt) {
            case 0:
                items = new String[]{"计划拒绝", "计划取消", "计划执行"};
                break;
            case 1:
                items = new String[]{"取消完成"};
                break;
            case 2:
                items = new String[]{"取消执行", "计划暂停", "执行完成"};
                break;
            case 4:
                items = new String[]{"暂停恢复", "执行完成"};
                break;
            case 5:
                items = new String[]{"取消拒绝"};
                break;
            case 6:
                Toast.makeText(getApplicationContext(), "医嘱计划已取消！", Toast.LENGTH_SHORT).show();
                return;
        }
        if (items.length > 0) {
            AlertDialog.Builder mChooseDialogBuilder = new AlertDialog.Builder(getApplicationContext());
            mChooseDialogBuilder.setTitle("计划操作");
            final String[] finalItems = items;
            mChooseDialogBuilder.setItems(items, (dialog, which) -> {
                String text = finalItems[which];
                switch (text) {
                    case "计划拒绝":
//                        doGetJJYY(ksdm);

                        break;
                    case "取消拒绝":
                        showWarnDialog("是否要取消拒绝该计划？", "0", null);
                        break;
                    case "计划取消":
                        showWarnDialog("是否要取消该计划？", "6", null);
                        break;
                    case "计划执行":
                        String message;
                        String yzzt1 = "2";
                        if (ChooseHDBZ != null && ChooseHDBZ.equals("1")) {
                            message = "是否要执行该计划？";
                        } else {
                            message = "该医嘱尚未核对，是否继续执行？";
                        }
                        if (!ChooseTMBH.substring(0, 2).equals("SY")) {
                            yzzt1 = "1";
                        }
                        showWarnDialog(message, yzzt1, null);
                        break;
                    case "取消执行":
//                        showWarnDialog("是否要取消执行？", "0", null);
                        break;
                    case "计划暂停":
//                        showWarnDialog("是否要暂停该计划？", "4", null);
                        break;
                    case "暂停恢复":
//                        showWarnDialog("是否要恢复该计划？", "2", null);
                        break;
                    case "执行完成":
//                        showWarnDialog("是否要完成该计划？", "1", null);
                        break;
                    case "取消完成":
//                        String backYzzt = "0";
//                        if (ChooseTMBH.substring(0, 2).equals("SY")) {
//                            backYzzt = "2";
//                        }
//                        showWarnDialog("是否要取消完成？", backYzzt, null);
                        break;
                }
            }).show();
        }
    }

    //提示对话框
    private void showWarnDialog(String msg, final String yzzt, final String yyxh) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setMessage(msg);
        builder.setTitle("提示");
        builder.setPositiveButton("确认", (dialog, which) -> {
//            String[] parms = new String[]{myApplication.getUser().getYGDM(), ChooseTMBH, yzzt, yyxh};
//            new MyExecuteYZJHTask().execute(parms);
            dialog.dismiss();
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        builder.create().show();
    }

    // 拒绝原因选择对话框
    private void showDenyDialog(String[] items) {
        final String[] yyxh = new String[1];
        AlertDialog.Builder mChooseDialogBuilder = new AlertDialog.Builder(getApplicationContext());
        mChooseDialogBuilder.setTitle("拒绝原因");
        mChooseDialogBuilder.setSingleChoiceItems(items, 0, (dialog, which) -> {
//                yyxh[0] = mYYList.get(which).getYYXH();
        });
        mChooseDialogBuilder.setPositiveButton("确认", (dialog, which) -> {
//                String[] parms = new String[]{myApplication.getUser().getYGDM(), ChooseTMBH, "5", yyxh[0]};
//                new MyExecuteYZJHTask().execute(parms);
            dialog.dismiss();
        });
        mChooseDialogBuilder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        mChooseDialogBuilder.create().show();
    }

    //获取医嘱计划拒绝原因
    private void doGetJJYY(String ksdm) {
        showProgressDialog("获取数据中");
        jjyyCall = MyServiceUtil.getJJYYCall(ksdm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                cancelProgressDialog();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mYYList = gson.fromJson(result, new TypeToken<List<JJQXYY>>() {
                    }.getType());
                    if (mYYList != null && mYYList.size() > 0) {
                        List<String> yymcList = new ArrayList<>();
                        for (JJQXYY qxyy : mYYList
                                ) {
                            yymcList.add(qxyy.getYYMC());
                        }
                        String[] yyitems = yymcList.toArray(new String[yymcList.size()]);
                        showDenyDialog(yyitems);
                    }
                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                cancelProgressDialog();
                Toast.makeText(getApplicationContext(), "获取拒绝原因失败！", Toast.LENGTH_SHORT).show();
            }
        };
        jjyyCall.enqueue(callback);
    }
}
